package com.eke.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 过滤器过滤后台，判断是否登陆
 * @author LS
 *
 */
public class AdminFilter implements Filter {
	
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute("userName");
//		Integer role = (Integer) session.getAttribute("userRole");
		if (username != null) {
			chain.doFilter(request, resp);
		} else {
			response.setHeader("refresh", "0;" + request.getContextPath() + "/pages/error/error.jsp");
		}
	}

	
	public void destroy() {

	}
}
